package 多线程.ThreadLocal;

import java.util.Objects;

public class CurrentUserHolder {
    public static ThreadLocal<User> threadLocal=new ThreadLocal<>();

    public static void setUser(User user){
        threadLocal.set(user);
    }

    public static User getUser(){
        if (Objects.nonNull(threadLocal.get())) {
            return threadLocal.get();
        }
        throw new RuntimeException("当前用户信息为空！");
    }

    public static void clearUser(){
        threadLocal.remove();
    }
}